package com.iye.puretennis.activity;

import android.app.Activity;
import android.content.ClipData;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.*;
import android.widget.*;
import com.iye.puretennis.R;
import com.iye.puretennis.custom.CustomGridView;
import com.iye.puretennis.custom.HuaWenHuPoTextView;
import com.iye.puretennis.custom.draggablegridview.BaseDynamicGridAdapter;
import com.iye.puretennis.custom.draggablegridview.DynamicGridView;
import com.iye.puretennis.domain.ListItem;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: jing wu
 * Date: 2015/1/16
 * Time: 11:10
 */
public class DragAndDropActivity extends Activity {

    public static final String TAG = "DragAndDropActivity";

    private LinkedList<ListItem> items1 = new LinkedList<ListItem>();

    private LinkedList<ListItem> items2 = new LinkedList<ListItem>();

    private LinkedList<ListItem> items3 = new LinkedList<ListItem>();


    private CustomGridView gridView1;

    private GridView gridView2;

    private CustomGridView gridView3;

    private View joinersActions;

    private View invitationActions;

    private View requestActions;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.draganddrop);

//        gridView1 = (CustomGridView) findViewById(R.id.view1);
//
//        gridView2 = (GridView) findViewById(R.id.view2);
//
//        gridView3 = (CustomGridView) findViewById(R.id.view3);

        joinersActions = findViewById(R.id.joiners_actions);

        requestActions = findViewById(R.id.request_actions);

        invitationActions = findViewById(R.id.invitation_actions);

        items1.add(new ListItem("1", "1", "so"));
        items1.add(new ListItem("2", "2", "like"));
        items1.add(new ListItem("3", "3", "do"));
        items1.add(new ListItem("4", "4", "what"));
        items1.add(new ListItem("5", "5", "that"));
        items1.add(new ListItem("6", "6", "need"));
        items1.add(new ListItem("7", "7", "main"));
        items1.add(new ListItem("8", "8", "appointment"));
        items1.add(new ListItem("9", "9", "dota"));

        items2.add(new ListItem("10", "10", "cool"));
        items2.add(new ListItem("11", "11", "are"));
        items2.add(new ListItem("12", "12", "then"));

        items3.add(new ListItem("13", "13", "nickname"));
        items3.add(new ListItem("14", "14", "photo"));
        items3.add(new ListItem("15", "15", "drag"));
        items3.add(new ListItem("16", "16", "and"));
        items3.add(new ListItem("17", "17", "drop"));

        final AppointmentAdapter adapter1 = new AppointmentAdapter(this, items1, 4);
        gridView1.setAdapter(adapter1);
        final AppointmentAdapter adapter2 = new AppointmentAdapter(this, items2, 4);
        gridView2.setAdapter(adapter2);
        final AppointmentAdapter adapter3 = new AppointmentAdapter(this, items3, 4);
        gridView3.setAdapter(adapter3);

        gridView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


                if (adapter1.getSelection().contains(position)) {
                    adapter1.removeSelection(position);
                } else {
                    adapter1.addSelection(position);
                }

                if(!adapter2.getSelection().isEmpty()){
                    adapter2.clearSelection();

                    adapter2.notifyDataSetChanged();
                }

                if (!adapter3.getSelection().isEmpty()) {
                    adapter3.clearSelection();
                    adapter3.notifyDataSetChanged();
                }
                adapter1.notifyDataSetChanged();

                invitationActions.setVisibility(adapter1.getSelection().size()>0?View.VISIBLE:View.INVISIBLE);
                joinersActions.setVisibility(View.GONE);
                requestActions.setVisibility(View.GONE);
            }
        });

        gridView1.setTargetView(gridView2);
        gridView1.setOnDropListener(new CustomGridView.OnDropListener() {
            @Override
            public void onDrop(int dragPosition) {
                ListItem item = items1.get(dragPosition);
                adapter1.remove(item);
                adapter2.add(item);
            }
        });



        gridView2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


                if (adapter2.getSelection().contains(position)) {
                    adapter2.removeSelection(position);
                } else {
                    adapter2.addSelection(position);
                }

                if(!adapter1.getSelection().isEmpty()){
                    adapter1.clearSelection();

                    adapter1.notifyDataSetChanged();
                }

                if (!adapter3.getSelection().isEmpty()) {
                    adapter3.clearSelection();
                    adapter3.notifyDataSetChanged();
                }
                adapter2.notifyDataSetChanged();
                joinersActions.setVisibility(adapter2.getSelection().size()>0?View.VISIBLE:View.INVISIBLE);
                invitationActions.setVisibility(View.GONE);
                requestActions.setVisibility(View.GONE);
            }
        });

        gridView3.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


                if (adapter3.getSelection().contains(position)) {
                    adapter3.removeSelection(position);
                } else {
                    adapter3.addSelection(position);
                }

                if(!adapter1.getSelection().isEmpty()){
                    adapter1.clearSelection();

                    adapter1.notifyDataSetChanged();
                }

                if (!adapter2.getSelection().isEmpty()) {
                    adapter2.clearSelection();
                    adapter2.notifyDataSetChanged();
                }
                adapter3.notifyDataSetChanged();
                requestActions.setVisibility(adapter3.getSelection().size()>0?View.VISIBLE:View.INVISIBLE);
                invitationActions.setVisibility(View.GONE);
                joinersActions.setVisibility(View.GONE);
            }
        });



    }


    class AppointmentAdapter extends BaseDynamicGridAdapter {

        private List<Integer> selection = new ArrayList<Integer>();


        public AppointmentAdapter(Context context, List<?> items1, int columnCount) {
            super(context, items1, columnCount);
        }

        @Override
        public View getView(int position, View view, ViewGroup viewGroup) {

            final ListItem listItem = (ListItem) getItem(position);
            TextView nicknameView;
            View imageView;
            ImageView selectedIconView;
            ViewCache viewCache;
            if (view == null) {
                view = LayoutInflater.from(getContext()).inflate(R.layout.user_image, null);
                nicknameView = (TextView) view.findViewById(R.id.user_nickname);
//                imageView = view.findViewById(R.id.user_image);
//                selectedIconView = (ImageView) view.findViewById(R.id.icon_yes);
//                viewCache = new ViewCache(nicknameView, imageView, selectedIconView);
//                view.setTag(viewCache);
            } else {
                viewCache = (ViewCache) view.getTag();
                imageView = viewCache.imageView;
                nicknameView = viewCache.nameView;
                selectedIconView = viewCache.selectedIconView;
            }
//            imageView.setBackgroundResource(R.drawable.icon);
            nicknameView.setText(listItem.getName());
//            if (getSelection().contains(position)) {
//                selectedIconView.setVisibility(View.VISIBLE);
//            } else {
//                selectedIconView.setVisibility(View.INVISIBLE);
//            }
            return view;
        }

        public List<Integer> getSelection() {
            return selection;
        }

        public void removeSelection(int position) {
            selection.remove(Integer.valueOf(position));
        }

        public void addSelection(int position) {
            selection.add(position);
        }

        public void clearSelection() {
            selection.clear();
        }
    }

    private class ViewCache {

        private TextView nameView;

        private View imageView;

        private ImageView selectedIconView;


        public ViewCache(TextView nameView, View imageView, ImageView selectedIconView) {
            this.nameView = nameView;
            this.imageView = imageView;
            this.selectedIconView = selectedIconView;
        }
    }



}
